package smlab.view.ui
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import mx.utils.DisplayUtil;
	
	import smlab.utils.DisplayObjectUtil;
	import smlab.utils.PopUpUtils;
	import smlab.view.ui.UIEvent.UIEvent;
	
	public class LoadingPanel_SM extends UIComponent_SM
	{
		private var closeBtn:SimpleButton;
		private var txt:TextField;
		private var loadingString_:String="";					//自己设定的类似：“正在加载...“的文字
		private var percentMC:MovieClip;
		private var _showLoadingTimeOut:uint;
		private var _showLoadingDelay:Number=200;//加载时延迟显示 loading 的时间。避免加载已经存在于IE缓存里的UI时也看见 Loaing
		
		public function LoadingPanel_SM()
		{
			super();
		}
		
		public function set showLoadingDelay(d:Number):void
		{
			if(d<0){
				d=0;
			}
			
			_showLoadingDelay=d;
		}
		
		public function get showLoadingDelay():Number
		{
			return _showLoadingDelay;
		}
		
		public function showCloseBtn(b:Boolean=true):void
		{
			closeBtn.visible=b;
		}
		
		override public function set skin(d:DisplayObject):void
		{
			super.skin=d;
			txt=Sprite(skin).getChildByName("txt") as TextField;
			closeBtn=Sprite(skin).getChildByName("closeBtn") as SimpleButton;
			var sp:Sprite=Sprite(skin).getChildByName("perMC") as Sprite;
			percentMC=sp.getChildByName("perMC") as MovieClip;
		}
		
		override public function show():void
		{
			if(closeBtn){
				closeBtn.addEventListener(MouseEvent.CLICK,onCloseBtnClick);
			}
			
			PopUpUtils.centerInStage(this);//位于stage居中
			this.skin.visible=false;
			super.show();
			_showLoadingTimeOut=setTimeout(showLoading,_showLoadingDelay);
		}
		
		private function showLoading() : void
		{
			this.skin.visible=true;
		}
		
		override public function hide():void
		{
			if(closeBtn){
				closeBtn.removeEventListener(MouseEvent.CLICK,onCloseBtnClick);
			}
			super.hide();
			clearTimeout(_showLoadingTimeOut);
		}
		
		public function onCloseBtnClick(e:MouseEvent):void
		{
			clearTimeout(_showLoadingTimeOut);
			dispatchEvent(new UIEvent(UIEvent.CANCEL_LOADING));
		}
		
		
		/**
		 * 设置进度条
		 * @param bytesLoaded
		 * @param bytesTotal
		 * @param textColor
		 * @param newLoadingStr 重设百分比前面的文字
		 * 
		 */
		public function setPercent(bytesLoaded:Number=0,bytesTotal:Number=0,loadingStr:String=" ",textColor:uint=0xffffff):void
		{
			var percentStr:String;
			
			if(loadingStr!=" "){
				loadingString_=loadingStr;
			}
			
			if(bytesLoaded==0 && bytesTotal==0){
				percentStr="";
			}else{
				percentStr="  "+int(bytesLoaded/bytesTotal*100)+"%";
			}
			
			if(this.isShow)
			{
				setPercentMC(bytesLoaded,bytesTotal);
				if(loadingStr==" "){
					setTxt(loadingString_+percentStr,textColor);
				}else{
					setTxt(loadingStr+percentStr,textColor);
				}
			}
		}
		
		/**
		 * 设置进度条上的进度MC跳帧
		 * @param bytesLoaded
		 * @param bytesTotal
		 * 
		 */
		private function setPercentMC(bytesLoaded:Number=0,bytesTotal:Number=0):void
		{
			var frame:int=int(percentMC.totalFrames*(bytesLoaded/bytesTotal));
			percentMC.gotoAndStop(frame);
		}
		
		private function setTxt(str:String="",textColor:uint=0xffffff):void
		{
			if(txt){
				txt.textColor=textColor;
				txt.text=str;
			}
		}
	}
}